package com.aispeech.detector.view.voiceView;

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RelativeLayout;

import com.aispeech.detector.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

public class MicAmplitudeView extends RelativeLayout {
    private static final String TAG = "MicAmplitudeView";
    private final int CHANNELS_MAX = 16;
    private final int REFRESH_CYCLE = 380;
    private final int MSG_UPDATE_AMP = 1;
    private final int MSG_UPDATE_CHANNELS = 2;

    private List<VoiceLineView> mVoiceLineViewList;

    private Handler mHandler;
    private boolean isAlive = false;
    private int mChannels = 1;


    public MicAmplitudeView(Context context) {
        super(context);
        init(null);
    }

    public MicAmplitudeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public MicAmplitudeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public MicAmplitudeView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(attrs);
    }

    private void initHandler() {
        final Random rand = new Random();
        mHandler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG_UPDATE_AMP:
//                        mVLineCh1.setVolume((int) (rand.nextInt(200)));
//                        mVLineCh2.setVolume((int) (rand.nextInt(200)));
//                        mVLineCh3.setVolume((int) (rand.nextInt(200)));
//                        mVLineCh4.setVolume((int) (rand.nextInt(200)));
                        break;
                    case MSG_UPDATE_CHANNELS:
                        updateChannels();
                        break;
                }
            }
        };
    }

    private void updateChannels() {
        Log.d(TAG, "updateChannels: mChannels = " + mChannels);
        for (int i = 0; i < CHANNELS_MAX; ++i) {
            if (i < mChannels) {
                mVoiceLineViewList.get(i).setVisibility(VISIBLE);
            } else {
                mVoiceLineViewList.get(i).setVisibility(GONE);
            }
        }
    }

    private void init(AttributeSet attrs) {
        inflate(getContext(), R.layout.mic_amplitude_view, this);
        mVoiceLineViewList = new ArrayList<>(CHANNELS_MAX);
        mVoiceLineViewList.add((VoiceLineView) findViewById(R.id.vl_ch1));
        mVoiceLineViewList.add((VoiceLineView) findViewById(R.id.vl_ch2));
        mVoiceLineViewList.add((VoiceLineView) findViewById(R.id.vl_ch3));
        mVoiceLineViewList.add((VoiceLineView) findViewById(R.id.vl_ch4));
        mVoiceLineViewList.add((VoiceLineView) findViewById(R.id.vl_ch5));
        mVoiceLineViewList.add((VoiceLineView) findViewById(R.id.vl_ch6));
        mVoiceLineViewList.add((VoiceLineView) findViewById(R.id.vl_ch7));
        mVoiceLineViewList.add((VoiceLineView) findViewById(R.id.vl_ch8));
        mVoiceLineViewList.add((VoiceLineView) findViewById(R.id.vl_ch9));
        mVoiceLineViewList.add((VoiceLineView) findViewById(R.id.vl_ch10));
        mVoiceLineViewList.add((VoiceLineView) findViewById(R.id.vl_ch11));
        mVoiceLineViewList.add((VoiceLineView) findViewById(R.id.vl_ch12));
        mVoiceLineViewList.add((VoiceLineView) findViewById(R.id.vl_ch13));
        mVoiceLineViewList.add((VoiceLineView) findViewById(R.id.vl_ch14));
        mVoiceLineViewList.add((VoiceLineView) findViewById(R.id.vl_ch15));
        mVoiceLineViewList.add((VoiceLineView) findViewById(R.id.vl_ch16));

        initHandler();

        //test
//        isAlive = true;
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                while (isAlive) {
//                    mHandler.sendEmptyMessage(MSG_UPDATE_AMP);
//                    try {
//                        Thread.sleep(REFRESH_CYCLE);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//        }).start();
    }

    public void setAlive(boolean alive) {
        isAlive = alive;
        if (alive) {
            for (int i = 0; i < mChannels; ++i) {
                mVoiceLineViewList.get(i).start();
            }
        } else {
            for (int i = 0; i < mChannels; ++i) {
                mVoiceLineViewList.get(i).stop();
            }
        }
    }

    public void setChannelsAmp(int channelIndex, int volume) {
        mVoiceLineViewList.get(channelIndex).setVolume(volume);
    }

    public void setChannels(int channels) {
        if (channels < 0) {
            channels = 0;
        }

        if (channels > CHANNELS_MAX) {
            channels = CHANNELS_MAX;
        }

        if (isAlive || mChannels == channels) {
            Log.w(TAG, "setChannels: no need set alive = " + isAlive + " mChannels = " + mChannels);
        } else {
            mChannels = channels;
            mHandler.sendEmptyMessage(MSG_UPDATE_CHANNELS);
        }
    }

}
